package com.szt.sy.service.system.role;

import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.szt.sy.entity.system.Role;
import com.szt.sy.mapper.system.RoleMapper;

/***
 * 角色服务
 * 
 * @author wwy
 *
 */
@CacheConfig(cacheNames = "cache.role")
@Service("roleService")
@Transactional(rollbackFor = RuntimeException.class)
public class RoleService extends ServiceImpl<RoleMapper, Role> {
	
	/**
	 * 列出此组下级角色
	 *
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Cacheable
	public List<Role> listAllRolesByPId(String parentId) throws Exception {
		return baseMapper.selectList(new EntityWrapper<Role>().eq("parentId", parentId).orderBy("addDate", true));
	}
	
	/***
	 * 列出所有不是顶级的角色
	 *
	 * @return
	 */
	@Cacheable
	public List<Role> listAllNotTopRole() {
		return baseMapper.selectList(new EntityWrapper<Role>().ne("parentId", "0").orderBy("addDate", true));
	}
	
	/**
	 * 通过id查找
	 *
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Cacheable
	public Role findObjectById(String id) throws Exception {
		return baseMapper.selectById(id);
	}
	
	/**
	 * 添加
	 *
	 * @param pd
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void add(Role entity) throws Exception {
		
		baseMapper.insert(entity);
	}
	
	/**
	 * 保存修改
	 *
	 * @param pd
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void edit(Role entity) throws Exception {
		baseMapper.updateById(entity);
	}
	
	/**
	 * 删除角色
	 *
	 * @param ROLE_ID
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void deleteRoleById(String id) throws Exception {
		baseMapper.deleteById(id);
	}
	
	/**
	 * 给当前角色附加菜单权限
	 *
	 * @param role
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void updateRoleRights(String id, String rights) throws Exception {
		Role role = new Role();
		role.setRights(rights);
		baseMapper.update(role, new EntityWrapper<Role>().eq("id", id));
	}
	
	/**
	 * 通过id查找
	 *
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	@Cacheable
	public Role getRoleById(String id) throws Exception {
		return baseMapper.selectById(id);
	}
	
	/**
	 * 给全部子角色加菜单权限
	 *
	 * @param pd
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void setAllRights(String rights, String id) throws Exception {
		Role role = new Role();
		role.setRights(rights);
		baseMapper.update(role, new EntityWrapper<Role>().eq("parentId", id));
	}
	
	/**
	 * 权限(增删改查)
	 *
	 * @param msg
	 *            区分增删改查
	 * @param pd
	 * @throws Exception
	 */
	@CacheEvict(allEntries = true)
	public void saveB4Button(String msg, String id, String value) throws Exception {
		Role role = new Role();
		if ("add_qx".equals(msg)) {
			role.setAdds(value);
			baseMapper.update(role, new EntityWrapper<Role>().eq("id", id));
		} else if ("del_qx".equals(msg)) {
			role.setDels(value);
			baseMapper.update(role, new EntityWrapper<Role>().eq("id", id));
		} else if ("edit_qx".equals(msg)) {
			role.setEdits(value);
			baseMapper.update(role, new EntityWrapper<Role>().eq("id", id));
		} else if ("cha_qx".equals(msg)) {
			role.setQuerys(value);
			baseMapper.update(role, new EntityWrapper<Role>().eq("id", id));
		}
	}
	
}
